package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.CommodityDao;
import cn.hs.ph.entity.Commodity;
import cn.hs.ph.util.JDBCDruidUtil;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommodityDaoImpl implements CommodityDao {
    @Override
    public List<Commodity> selectAll(Map<String, String> map) {
        List<Commodity> list=new ArrayList<>();
        String sql="select * from commodity where 1=1 ";
        List params=new ArrayList();
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        String goodsShopName = map.get("shopName");
        if (goodsShopName!=null&&!"".equals(goodsShopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+goodsShopName+"%");
        }
        String startTime =map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        sql+=" order by goodsId desc ";
        //判断是否有分页
        String pn = map.get("pageNum");
        if (pn!=null&&!"".equals(pn)){
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        ResultSet resultSet = JDBCDruidUtil.query(sql, params.toArray());
        try {
            while (resultSet.next()){
            Commodity commodity=new Commodity(
                    resultSet.getInt(1),
                    resultSet.getString(2),
                    resultSet.getString(3),
                    resultSet.getInt(4),
                    resultSet.getString(5),
                    resultSet.getString(6),
                    resultSet.getString(7),
                    resultSet.getDate(8),
                    resultSet.getString(9),
                    resultSet.getString(10),
                    resultSet.getString(11),
                    resultSet.getString(12)
            );
            list.add(commodity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count=0;
        String sql=" select count(1) from commodity where 1=1 ";
        List params=new ArrayList<>();
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        String goodsShopName = map.get("shopName");
        if (goodsShopName!=null&&!"".equals(goodsShopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+goodsShopName+"%");
        }
        String startTime =map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        sql+=" order by goodsId desc ";
        ResultSet resultSet = JDBCDruidUtil.query(sql, params.toArray());
        try {
            while (resultSet.next()){
              count= resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JDBCDruidUtil.close();
        return count;
    }
}
